#include "HttpServer.hpp"
#include <memory>
using namespace std;
using namespace server;

//http不用自己写客户端了，因为已经存在，直接使用浏览器就可以了
void Usage(std::string proc)
{
    cerr << "Usage:\n\t" << proc << " port\r\n\r\n";
}

// 1. 服务器和网页分离
// 2. url -> / :web根目录
bool Get(const HttpRequest &req, HttpResponse &resp)
{
    // for test
    cout << "--------------------------http start------------------------------" << endl;
    cout << req.inbuffer << endl;
    std::cout << "method: " << req.method << std::endl; 
    std::cout << "url: " << req.url << std::endl; 
    std::cout << "httpversion: " << req.httpversion << std::endl; 
    std::cout << "path: " << req.path << std::endl; 
    cout << "--------------------------http end------------------------------" << endl;

    std::string respline = "Http/1.1 200 OK\r\n";
    std::string respheader = "Content-Type: text/html\r\n";
    std::string respblank = "\r\n";
    std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>hello world</h1></head><body><p>新华社北京6月1日电（记者阳娜、温竞华）我国首个国际科技组织总部集聚区5月31日在北京揭牌并正式启用。该集聚区由中国科协与北京市政府合作共建，首批已迎来国际动物学会、国际数字地球学会、国际氢能燃料电池协会等8家国际科技组织入驻。</p></body></html>";
    
    resp.outbuffer += respline;
    resp.outbuffer += respheader;
    resp.outbuffer += respblank;
    resp.outbuffer += body;

    return true;
}


// ./httpServer 8080
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }

    uint16_t port = atoi(argv[1]);
    unique_ptr<HttpServer> httpsvr(new HttpServer(Get, port));
    httpsvr->initServer();
    httpsvr->start();
}

